/*******************************************************************************
 * Copyright (c) 2013 University of Luxembourg and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     @author Qin ZHANG ("Jackin" as a Global Communication ID)
 ******************************************************************************/
package lu.uni.geko.commands;

import java.util.ArrayList;
import java.util.List;

import lu.uni.geko.ext.ram.WeaverExt;

import org.eclipse.core.resources.IFile;
import org.eclipse.emf.common.util.URI;

/**
 * @author Jackin
 * 
 * This is an extended handler for the weave command specialized for
 * TouchRAM weaving function.
 */
public class WeaveRamExtHandler extends WeaveInferringPc2AvMappingHandler{
	/** base model file type ID. */
	private static final int BASE_FILE_TYPE = 0;
	
	/** 
	 * Number of distinct file types. 
	 * Note that TouchRAM only uses aspects (base) as modeling and weaving units.
	 */
	private static final int TYPE_COUNT = 1;

	@Override
	protected int getTypeCount() {
	   return TYPE_COUNT;
	}

	@Override
	protected int getTypeNoIfSelectable(final IFile iFile) {
	  String fileExt = iFile.getFileExtension();
	  if (fileExt.equals("ram")) {
	    return BASE_FILE_TYPE;
	  } else {
	     return super.getTypeNoIfSelectable(iFile);
	  }
	}

	@Override
	protected List<Runnable> getRunnables(final List<List<URI>> uris) {
		if (uris.size() == getTypeCount()) {
			List<URI> baseMURIs = uris.get(BASE_FILE_TYPE);
			List<Runnable> runnables = new ArrayList<Runnable>(baseMURIs.size());
			for (final URI baseMURI : baseMURIs) {
					Runnable runnable = getRunnable(baseMURI);
					runnables.add(runnable);
			}
			return runnables;
		} else {
			throw new RuntimeException(this.getClass().getName() + " needs RAM aspect model!");
		}
	}
	
	protected Runnable getRunnable(final URI baseMURI) {
	   return new Runnable() {
	      @Override
	      public void run() {
	      	WeaverExt weaver = new WeaverExt(baseMURI);
	   	    weaver.weave(baseMURI);
	      }
	   };
	}
}
